package com.hongdee.scm.invioce.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.invioce.entity.TCrmCarInvoice;
import com.hongdee.scm.invioce.service.ITCrmCarInvoiceService;
import com.hongdee.scm.saleorder.service.ITScmSaleOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 名  称:TCrmCarInvoiceController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.crm.invioce.controller
 * 作  者: 术洪超
 * 日  期:2017-07-04 20:58
 */
@RestController
@RequestMapping("/scm/invoice")
public class TCrmCarInvoiceController {

    @Autowired
    private ITCrmCarInvoiceService itCrmCarInvoiceService;
    @Autowired
    ITScmSaleOrderService itScmSaleOrderService;


    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加的实体内容
     * @author 术洪超
     */

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "taxcode", defaultValue = "") String taxcode,
                             @RequestParam(name = "orgcode", defaultValue = "") String orgcode,
                             @RequestBody TCrmCarInvoice data) throws Exception {
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(false);

        if (null == data)
        {
            res.setMsg("参数不对，未获取对象数据！");
            return res;
        }
        if (StringUtils.isEmpty(data.getCJHM()))
        {
            res.setMsg("参数不对，车架号为空！");
            return res;
        }
        if (StringUtils.isEmpty(data.getFPHM()))
        {
            res.setMsg("参数不对，发票号为空！");
            return res;
        }

        if (StringUtils.isEmpty(data.getGHDW()))
        {
            res.setMsg("参数不对，购货单位为空！");
            return res;
        }

        data.setTaxCode(taxcode);
        data.setCompanyId(orgcode);
        boolean rs = this.itCrmCarInvoiceService.saveOrUpdate(data, "");
        res.setSuccess(rs);
        if (!rs)
        {
            res.setMsg("保存失败，后台异常");
        }
        itScmSaleOrderService.createBySaleInvoice(data);
        return res;


    }

}
